<template>
  <!-- 面包屑 -->
  <div>
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item>课程中心</el-breadcrumb-item>
      <el-breadcrumb-item><b>本学期课程</b></el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <!-- 数据卡片 -->
  <div class="cards-container">
    <div v-for="course in tableCourse" :key="course.cid" class="card-container" @click="saveCidAndGoToDetail(course.cid)">
      <el-card>
        <div class="card-content">
          <h3>{{ course.cname }}</h3>
          <p>课程号: {{ course.cid }}</p>
          <p>上课时间: {{ course.ltime }}</p>
          <p>上课地点: {{ course.location }}</p>
          <p>课时数: {{ course.chour }}</p>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import request from "@/utils/class";
import { useRouter } from 'vue-router';

// 表格信息显示
const tableCourse = ref([]);

const router = useRouter();

onMounted(() => {
  getCourse();
});

const getCourse = () => {
  // 从 sessionStorage 获取 tid
  const loginUser = sessionStorage.getItem('loginUser');
  const user = JSON.parse(loginUser);
  const tid = user.tid;

  request({
    url: '/tselect/selfcourse',
    method: 'get',
    params: {
      tid: tid
    }
  }).then(response => {
    if (response.data.errorCode === 200) {
      tableCourse.value = response.data.data;
    } else {
      console.error('Failed to fetch course data:', response.data);
    }
  }).catch(error => {
    console.error('Network or other error occurred:', error);
  });
};

// 点击卡片跳转至课程详情的方法，并保存 cid 到 sessionStorage
const saveCidAndGoToDetail = (cid) => {
  sessionStorage.setItem('selectedCid', cid);
  router.push({ path: `/teacher/course/detail?cid=${cid}` });
};
</script>

<style scoped>
.el-breadcrumb {
  font-size: 18px;
  margin: 20px;
}
.cards-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  margin: 40px 20px 20px 20px;
}

.card-container {
  width: calc(33% - 20px);
  margin: 10px;
  cursor: pointer;
  transition: transform 0.3s ease-in-out;
}

.card-container:hover .el-card {
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.card-content h3 {
  margin: 0 0 10px;
  color: #333;
}

.card-content p {
  margin: 5px 0;
  color: #666;
}

.el-card {
  width: 100%;
  border-radius: 10px;
  overflow: hidden;
}

@media (max-width: 1200px) {
  .card-container {
    width: calc(50% - 20px);
  }
}

@media (max-width: 600px) {
  .card-container {
    width: 100%;
  }
}
</style>